package com.lvy.hczn.transmit.controller;

import com.lvy.hczn.front.common.annotation.Log;
import com.lvy.hczn.front.common.core.controller.BaseController;
import com.lvy.hczn.front.common.core.domain.AjaxResult;
import com.lvy.hczn.front.common.core.page.TableDataInfo;
import com.lvy.hczn.front.common.enums.BusinessType;
import com.lvy.hczn.front.common.utils.poi.ExcelUtil;
import com.lvy.hczn.transmit.domain.TemperAreaConfig;
import com.lvy.hczn.transmit.service.TemperAreaConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/system/temper")
@Slf4j
public class TemperAreaController extends BaseController {

    private String prefix = "system/temper";

    @Autowired
    private TemperAreaConfigService temperAreaConfigService;

    @RequiresPermissions("system:temper:view")
    @GetMapping()
    public String temper() {
        return prefix + "/temper";
    }

    /**
     * 查询温区配置列表
     */
    @RequiresPermissions("system:temper:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TemperAreaConfig temperAreaConfig) {
        startPage();
        List<TemperAreaConfig> list = temperAreaConfigService.selectTemperAreaConfigList(temperAreaConfig);
        return getDataTable(list);
    }

    /**
     * 导出温区配置列表
     */
    @RequiresPermissions("system:temper:export")
    @Log(title = "温区配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TemperAreaConfig temperAreaConfig) {
        List<TemperAreaConfig> list = temperAreaConfigService.selectTemperAreaConfigList(temperAreaConfig);
        ExcelUtil<TemperAreaConfig> util = new ExcelUtil<TemperAreaConfig>(TemperAreaConfig.class);
        return util.exportExcel(list, "温区配置数据");
    }

    /**
     * 新增温区配置
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存温区配置
     */
    @RequiresPermissions("system:temper:add")
    @Log(title = "温区配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TemperAreaConfig temperAreaConfig) {
        return toAjax(temperAreaConfigService.insertTemperAreaConfig(temperAreaConfig));
    }

    /**
     * 修改温区配置
     */
    @RequiresPermissions("system:temper:edit")
    @GetMapping("/edit/{temperAreaId}")
    public String edit(@PathVariable("temperAreaId") String temperAreaId, ModelMap mmap) {
        TemperAreaConfig temperAreaConfig = temperAreaConfigService.selectTemperAreaConfigByTemperAreaId(temperAreaId);
        mmap.put("temperAreaConfig", temperAreaConfig);
        return prefix + "/edit";
    }

    /**
     * 修改保存温区配置
     */
    @RequiresPermissions("system:temper:edit")
    @Log(title = "温区配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TemperAreaConfig temperAreaConfig) {
        try {
            return toAjax(temperAreaConfigService.updateTemperAreaConfig(temperAreaConfig));
        } catch (Exception e) {
            log.error("editSave：温区配置修改：{}", e);
            return AjaxResult.error("修改异常");
        }
    }

    /**
     * 删除温区配置
     */
    @RequiresPermissions("system:temper:remove")
    @Log(title = "温区配置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(temperAreaConfigService.deleteTemperAreaConfigByTemperAreaIds(ids));
    }
}
